package com.study.Util;

import com.study.common.ReturnCode;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Data
@NoArgsConstructor
@Slf4j
public class CommonResponse<T> {


    private String requestId;
    //openapi接口使用，否则requestId在返回时会被复写
    private String requestIdStr;

    private String code;

    private String msg;

    private T data;

    public static CommonResponse success() {
        return new CommonResponse<>(1, ReturnCode.SUCCESS, "成功");
    }

    public static <T> CommonResponse<T> success(T data) {
        return new CommonResponse<>(data, ReturnCode.SUCCESS, "成功");
    }

    public static <T> CommonResponse<T> fail(T data, String code) {
        return new CommonResponse<>(data, code, "");
    }

    public static <T> CommonResponse<T> fail(T data, String code, String message) {
        return new CommonResponse<>(data, code, message);
    }

    public static <T> CommonResponse<T> fail(String message) {
        return new CommonResponse<>(null, ReturnCode.FAILED, message);
    }

    private CommonResponse(T data, String code, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public boolean isSuccess() {
        return ReturnCode.SUCCESS.equals(this.code);
    }

}
